DIR=$(pwd)
###
 # @Author: Lai Jun
 # @E-mail: laijun@nudt.edu.cn
 # @LastEditors: Lai Jun
 # @LastEditTime: 2022-06-27 10:22:16
### 
echo "设置github与gitee的双重git ssh秘钥"
if [ ! -d $HOME/.ssh ];then
    echo "文件夹 $HOME/.ssh 不存在，需要建立吗？"
    read -p "y or Y for yes, otherwise no" yes
    if [[ $yes == "y" || $yes == "Y" ]]; then
        mkdir -p $HOME/.ssh
    else
        exit 0
    fi
fi
cd $HOME/.ssh
echo "输入你的github邮箱: Enter the your github email: "
read EMAIL_GITHUB
ssh-keygen -t rsa -C "$EMAIL_GITHUB" -f id_rsa_github

echo "输入你的gitee邮箱: Enter the your gitee email: "
read EMAIL_GITEE
ssh-keygen -t rsa -C "$EMAIL_GITEE" -f id_rsa_gitee

ssh-add $HOME/.ssh/id_rsa_github
ssh-add $HOME/.ssh/id_rsa_gitee

config_str="#网站的别名\n
Host github.com
# 托管网站的域名\n
HostName github.com\n
#指定优先使用哪种方式验证, 支持密码和秘钥验证方式\n
PreferredAuthentications publickey\n
# 托管网站上的用户名, 最好写账户邮箱, 否则容易设置失败\n
User $EMAIL_GITHUB\n
# 使用的密钥文件\n
IdentityFile ~/.ssh/id_rsa_github"

config_str=$config_str"\n\n#网站的别名\n
Host gitee.com
# 托管网站的域名\n
HostName gitee.com\n
#指定优先使用哪种方式验证, 支持密码和秘钥验证方式\n
PreferredAuthentications publickey\n
# 托管网站上的用户名, 最好写账户邮箱, 否则容易设置失败\n
User $EMAIL_GITEE\n
# 使用的密钥文件\n
IdentityFile ~/.ssh/id_rsa_gitee"

echo -e $config_str > config

echo "复制以下公钥到github:"
cat $HOME/.ssh/id_rsa_github.pub

echo "复制以下公钥到gitee:"
cat $HOME/.ssh/id_rsa_gitee.pub

echo -e "验证方法：在命令行中输入 ssh -T git@github.com 或 ssh -T git@gitee.com "
cd $DIR